<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>渐变</title>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
  function $(id) {
    return document.getElementById(id);
  }

  // 设置线性渐变
  function initial() {
    var canvas = $('canvas');
    var ctx = canvas.getContext('2d');

    var grad = ctx.createLinearGradient(0, 400, 400, 400);
    grad.addColorStop(0, 'red');
    grad.addColorStop(.8, 'yellow');
    grad.addColorStop(1, 'blue');

    ctx.fillStyle = grad;
    ctx.fillRect(0, 0, 400, 400);
  }

  // 绘制一个圆形
  function arc() {
    var canvas = $('canvas');
    var ctx = canvas.getContext('2d');

    ctx.beginPath();
    ctx.arc(100, 100, 50, 0, Math.PI * 2, true);
    ctx.closePath();

    ctx.fillStyle = 'red';
    ctx.fill();

    ctx.beginPath()
    ctx.arc(250, 250, 50, Math.PI / 2, Math.PI * 3 / 2, false);
    ctx.fillStyle = 'red';
    ctx.fill();
  }


  window.addEventListener('load', arc, false)
</script>
</body>
</html>